package com.jinunn.hosp.controller;

import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinunn.common.result.Result;
import com.jinunn.common.utils.MD5;
import com.jinunn.hosp.service.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author : JinDun
 * @date : 2021/4/23 23:22
 */

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "添加")
    @PostMapping("save")
    public Result<HospitalSet> saveHosp(@RequestBody HospitalSet hospitalSet){
        //1可用，0不可用
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    @ApiOperation(value = "列表")
    @GetMapping("list")
    public Result<List<HospitalSet>> list(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }


    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result<HospitalSet> deleteHosp(@PathVariable("id") Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
           return Result.ok();
        }else {
           return Result.fail();
        }
    }

    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageHostset/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHost(@PathVariable("current") long current,
                               @PathVariable("limit") long limit,
                               @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> setServicePage = hospitalSetService.findPage(current, limit, hospitalSetQueryVo);
        return Result.ok(setServicePage);
    }

    @ApiOperation("详情")
    @GetMapping("detil/{id}")
    public Result<HospitalSet> getDetil(@PathVariable("id") long id){
        HospitalSet detil =hospitalSetService.getDetil(id);
        return Result.ok(detil);
    }

    @ApiOperation("修改")
    @PostMapping("updateHostset")
    public Result<HospitalSet> updateHostset(@RequestBody HospitalSet hospitalSet){
        boolean falg = hospitalSetService.updateHostset(hospitalSet);
        if (falg) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchDelete")
    public Result<HospitalSet> batchDelete(@RequestBody List<Long> ids){
        boolean remove = hospitalSetService.removeByIds(ids);
        if (remove){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result<HospitalSet> lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation("发送签名密钥")
    @GetMapping("lockHospitalSet/{id}")
    public Result<HospitalSet> lockHospitalSet(@PathVariable("id") Long id){
//        HospitalSet hospitalSet = hospitalSetService.getById(id);
//        String signKey = hospitalSet.getSignKey();
//        String hosname = hospitalSet.getHosname();
        //TODO

        return Result.ok();
    }

    @ApiOperation("获取医院签名信息")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(@PathVariable("hoscode") String hoscode){
        return hospitalSetService.getSignInfoVo(hoscode);
    }
}
